package com.starblink.search.category.ui

import android.view.LayoutInflater
import android.view.ViewGroup
import com.blankj.utilcode.util.FragmentUtils
import com.starblink.android.basic.base.fragment.BaseTVMFragment
import com.starblink.android.basic.ext.bindFragment
import com.starblink.android.basic.ext.bindSimpleTab
import com.starblink.android.basic.ext.click
import com.starblink.android.basic.ext.color
import com.starblink.android.basic.ext.dp2px
import com.starblink.android.basic.ext.enableCorner
import com.starblink.android.basic.ext.gone
import com.starblink.android.basic.ext.visible
import com.starblink.android.basic.ext.visibleOrGone
import com.starblink.android.basic.extension.apollo.id2String
import com.starblink.android.basic.extension.dp2px
import com.starblink.android.basic.extension.navigation
import com.starblink.android.basic.extension.navigationTo
import com.starblink.android.basic.sensorsdata.extension.trackData
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmElementDef
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmPageDef
import com.starblink.basic.route.RoutePage
import com.starblink.rocketreserver.fragment.CategoryF
import com.starblink.search.category.ui.main.CateMainVM
import com.starblink.search.category.ui.main.tab.CategoryGoodsTabFragment
import com.starblink.search.databinding.ActivityCategoryMainBinding
import com.starblink.search.databinding.LayoutCategoryOneTabLoadingBinding

/**
 * 搜索类目主页面
 */
class SearchCategoryFragment :
    BaseTVMFragment<ActivityCategoryMainBinding, CateMainVM>() {

//    var categoryId: String? = null
//    var title: String? = null

    companion object {
        fun obtainFragment(): SearchCategoryFragment {
            return SearchCategoryFragment()
        }

        const val TAB_ALL_ITEMS = 0
    }

    private var loadingFragment: CategoryGoodsTabFragment? = null

//    private lateinit var tabLoadingBinding: LayoutCategoryOneTabLoadingBinding

    fun initView() {

        //默认显示avfView,此处隐藏，显示text
        viewBinding.rlSearchBar.avfView.gone()
        viewBinding.rlSearchBar.avfViewText.visible()

        viewBinding.viewPager.enableCorner(dp2px(24f), "top")

        viewBinding.rlSearchBar.iCameraImg.trackData(
            element = SpmElementDef.HomeNewCameraClick_33001,
            page = SpmPageDef.CATEGORY_MAIN_GOODS_PAGE,
        )


        viewBinding.rlSearchBar.iCameraImg.click {
            navigation(path = RoutePage.ImageSearch.PAGE_CAMERA)
        }

        viewBinding.rlSearchBar.avfViewText.setOnClickListener {
            navigationTo(RoutePage.Search.PAGE_SEARCH) {
                //extraOf(RoutePage.Search.ARG_SCENE to RoutePage.Search.SCENE.GOODS.value)
            }
        }
        viewBinding.rlSearchBar.accmLogOut.setOnClickListener {
            navigationTo(RoutePage.Search.PAGE_SEARCH) {
                //extraOf(RoutePage.Search.ARG_SCENE to RoutePage.Search.SCENE.GOODS.value)
            }
        }
        viewBinding.vCategoryAll.trackData(
            element = SpmElementDef.HomeNewCategoryAll_33005,
            page = SpmPageDef.CATEGORY_MAIN_GOODS_PAGE,
            onlyHit = true,
        )
        viewBinding.vCategoryAll.setOnClickListener {
            navigationTo(RoutePage.Search.PAGE_GOODS_CATEGORY)
        }
        loadingFragment = CategoryGoodsTabFragment.newInstance(null, "")
        FragmentUtils.add(
            childFragmentManager,
            loadingFragment!!,
            viewBinding.loadingContainer.id,
            false,
            false
        )
    }

    private fun initTabs(list: List<CategoryF>) {
        viewBinding.groupTabs.visibleOrGone(list.isNotEmpty()) {
//            tabLoadingBinding.groupTabsLoading.gone()
            viewBinding.viewPager.bindFragment(
                childFragmentManager,
                count = list.size
            ) {
                CategoryGoodsTabFragment.newInstance(
                    list[it].id2String,
                    list[it].name,
                    it
                )
            }
            viewBinding.tabs.bindSimpleTab(
                list.map { it.name ?: "" },
                gap = 16.dp2px(),
                viewPager = viewBinding.viewPager,
                trackCall = { view, index ->
                    view.trackData(
                        element = SpmElementDef.Element_30060,
                        page = SpmPageDef.CATEGORY_MAIN_GOODS_PAGE,
                        params = mapOf(
                            "tabName" to (list.getOrNull(index)?.name ?: ""),
                        ),
                    )
                },
                indicator = color(com.starblink.basic.style.R.color.sk_gray0)
            )
//            categoryId?.let { id ->
//                list.indexOfFirst { it.id2String == id }.takeIf { it > -1}?.let {
//                    viewBinding.viewPager.currentItem = it
//                }
//            }
            viewBinding.viewPager.currentItem = 0
            viewBinding.loadingContainer.gone()
            loadingFragment?.popSelf()
        }
    }

    override fun onResume() {
        super.onResume()
//        pageExposure(SpmPageDef.CATEGORY_MAIN_GOODS_PAGE, params = mapOf(
//            "id" to (categoryId ?: ""),
//            "name" to (title ?: ""),
//        ))
    }

    override fun onPause() {
        super.onPause()
//        pageDisappear(
//            SpmPageDef.CATEGORY_MAIN_GOODS_PAGE,
//            params = mapOf(
//                "id" to (categoryId ?: ""),
//                "name" to (title ?: ""),
//            )
//        )
    }

    override fun getViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): ActivityCategoryMainBinding {
        return ActivityCategoryMainBinding.inflate(inflater, container, false)
    }

    override fun startObserve() {
        viewModel.contentLD.observe(this) { list ->
            initTabs(list)
        }
    }

    override fun initData() {
        initView()
        viewModel.loadIniData()
    }


}